package com.tjvib.adapter;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.tjvib.R;
import com.tjvib.bean.BLEDevice;
import com.tjvib.util.ToastUtil;
import com.tjvib.widget.ListIZltemOnClickListener;
import com.tjvib.widget.ListItemOnClickListener;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 搜索到的设备列表适配器
 */
public class LVDevicesAdapter extends BaseQuickAdapter<BLEDevice, BaseViewHolder> {

    private Context context;
    private List<BLEDevice> list = new ArrayList<>();
    private ListIZltemOnClickListener itemOnClickListener;

    public LVDevicesAdapter(int layoutResId, Context context, ListIZltemOnClickListener itemOnClickListener) {
        super(layoutResId);
        this.context = context;
        this.list = new ArrayList<>();
        this.itemOnClickListener = itemOnClickListener;
    }

    public LVDevicesAdapter(int layoutResId, Context context, @Nullable List<BLEDevice> data,
                            ListIZltemOnClickListener itemOnClickListener) {
        super(layoutResId, data);
        this.context = context;
        this.list = data;
        this.itemOnClickListener = itemOnClickListener;
    }


    /**
     * 初始化所有设备列表
     * @param bluetoothDevices
     */
    public void addAllDevice(List<BLEDevice> bluetoothDevices){
        if(list != null){
            list.clear();
            list.addAll(bluetoothDevices);
            notifyDataSetChanged();
        }
    }

    /**
     * 添加列表子项
     * @param bleDevice
     */
    public void addDevice(BLEDevice bleDevice) {
        // 检查传入的设备是否为null
        if (bleDevice == null || bleDevice.getBluetoothDevice() == null ||
                bleDevice.getBluetoothDevice().getName() == null) {
            return;
        }

        // 检查设备是否已经存在于列表中
        for (BLEDevice existingDevice : list) {
            if (existingDevice.getBluetoothDevice().getAddress().equals(bleDevice.getBluetoothDevice().getAddress())) {
                // 如果设备已存在，仅更新RSSI值
                existingDevice.setRSSI(bleDevice.getRSSI());
                notifyDataSetChanged(); // 刷新数据
                return;
            }
        }

        // 如果设备不存在，添加到列表中
        list.add(bleDevice);
        notifyDataSetChanged(); // 刷新数据
    }

    /**
     * 清空列表
     */
    public void clear(){
        if(list != null){
            list.clear();
        }
        notifyDataSetChanged(); //刷新
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, BLEDevice bleDevice) {
        baseViewHolder.setText(R.id.item_device_tv_name,
                                bleDevice.getBluetoothDevice().getName() == null ?
                                "NULL" : bleDevice.getBluetoothDevice().getName())
                        .setText(R.id.item_device_tv_ip, bleDevice.getBluetoothDevice().getAddress());

        CheckBox cb = baseViewHolder.findView(R.id.item_device_cb_choose);
        if (isConnect(bleDevice.getBluetoothDevice()))
            cb.setChecked(true);
        else
            cb.setChecked(false);

        cb.setOnClickListener(v -> itemOnClickListener.onClick(v, bleDevice));
    }

    private boolean isConnect(BluetoothDevice device){
        try {
            Method method = BluetoothDevice.class.getDeclaredMethod("isConnected", (Class[]) null);
            method.setAccessible(true);
            return (boolean) method.invoke(device, (Object[]) null);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }
}
